package com.service.Impl;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mapper.ResultMapper;
import com.model.Result;
import com.mapper.LoginMapper;
import com.service.LoginService;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.model.Login;
import com.util.BeanUtil;
import com.util.ExcelMultiWriter;
import com.util.ExcelMultiWriter.ExcelInfo;
import com.util.PagedRequest;
import com.util.PagedResult;
import com.github.pagehelper.PageHelper;
@Service("loginService")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class LoginServiceImpl implements LoginService {
	@Autowired
	LoginMapper loginMapper;
	@Autowired
	ResultMapper resultMapper;
	public Login getLogin(int id) {
		
		return loginMapper.getLogin(id);
	}

	
	
	public PagedResult<Login> getAllLogin(PagedRequest pagedRequest) {
	Map map = new HashMap();
/*		map.put("userName", userName);
		map.put("pps", pps);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("elementId", elementId);*/
		map.put("sord", pagedRequest.getSord());  //排序序名
		map.put("sidx", pagedRequest.getSidx());  //排序方式
		map.put("page", pagedRequest.getPage());  
		map.put("rows", pagedRequest.getRows()); 
	PageHelper.startPage(pagedRequest.getPage(),pagedRequest.getRows());  //分页参数
		return BeanUtil.toPagedResult(loginMapper.getAllLogin(map));
	}


	public int update(Login login) {
		
		return loginMapper.update(login);
	}

	
	public int add(Login login) {
		
		return loginMapper.add(login);
	}


	public int delete(int id) {
		
		return loginMapper.delete(id);
	}



	public HSSFWorkbook ext() throws IOException, RowsExceededException, WriteException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
		Map map = new HashMap();
				map.put("sord", "asc");  //排序序名
				map.put("sidx", "id");  //排序方式
				map.put("page", "1");  
				map.put("rows", "5"); 
		ExcelMultiWriter excelMultiWriter = new ExcelMultiWriter("D:/test1.xls");

		ExcelInfo excelInfo = new ExcelInfo();

		excelInfo.SheetName = "第%i页";

		excelInfo.Headers = new String[] { "id", "名字", "密码" };

		excelInfo.Propertys = new String[] { "id", "userName", "passWord" };

		excelInfo.ContentFormat = new String[] { null, null, null };
		 ArrayList []datas= new ArrayList[1];
		 datas[0]=(ArrayList) loginMapper.getAllLogin(map);
	
		
		return excelMultiWriter.write(datas, new ExcelInfo[]{excelInfo});
	}



	public String test() {
	
		return "测试成功";
	}
	public int anser_insert(Result r){
		return resultMapper.insert(r);
	}
	public List<Result > getAnserAll(){
		return resultMapper.getAll();
	}
}
